#=
bill:
- Julia version: 
- Author: v
- Date: 2021-04-18
=#

using CSV,DataFrames
using Plots
import Plots:plot

include("config.jl")

function data_to_gram(name, data)
    println(name)
    println(data[1])
    println(data[2])
    plot(data[1], data[2], label="line")
    scatter!(data[1], data[2], label="points")
    xlabel!("时间")
    ylabel!("金额")
    title!("流水")
    filename = string(t_configPath , "/" , name , ".png")
    println("save to ", filename)
    savefig(filename)
end

function data_from_csv(filename)
    return CSV.File(filename)
end

function data_parse(data)
    a_ze = []
    x1 = []
    x2 = []
    index = 1
    a_minu = []
    m1 = []
    m2 = []
    for i in eachrow(data)
        if i[t_ze] === missing
            # 用上一个减支加上，得到新的值
            l_ze = data[index-1, t_ze]
            sr = i[t_sr] === missing ? 0 : i[t_sr]
            zc = i[t_zc] === missing ? 0 : i[t_zc]
            x = l_ze + sr - zc
            push!(x1, x)
        else
            push!(x1, i[t_ze])
        end

        if index > 1
            push!(m1, i[t_ze] - x1[index-1])
            push!(m2, i[t_sj])
        end
        y = i[t_sj]
        push!(x2, y)
        index = index+1
    end
    push!(a_ze, x2)
    push!(a_ze, x1)
    push!(a_minu, m2)
    push!(a_minu, m1)
    return a_ze, a_minu
end

println("program start here ")
data = DataFrame(data_from_csv(ARGS[1]))

datagroup = groupby(data, [t_xm])

for sub in datagroup
    println(sub)
    x1, x2 = data_parse(sub)
    data_to_gram(sub[:, t_xm][1], x2)
end
